<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" class="search-form">
      <el-form-item label="所属流域" prop="riverBasinName">
        <el-input v-model="queryParams.riverBasinName" placeholder="请输入所属流域" clearable />
      </el-form-item>
      <el-form-item label="所属水系" prop="riverSystemName">
        <el-input v-model="queryParams.riverSystemName" placeholder="请输入所属水系" clearable />
      </el-form-item>
      <el-form-item label="河流名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入河流名称" clearable />
      </el-form-item>
      <el-form-item label="河流编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入编码" clearable />
      </el-form-item>
      <el-form-item label="是否在境内" prop="within">
        <el-select v-model="queryParams.within">
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery" style="margin-left: 20px">搜索
        </el-button>
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="plus" @click="handleAdd" v-hasPermi="['base:river:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" :loading="exportLoading" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="fetchList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riverList" border row-key="id" default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <!--<el-table-column label="ID" prop="id" width="120" />-->
      <el-table-column label="河流名称" align="left" header-align="center" prop="name" width="200" show-overflow-tooltip />
      <el-table-column label="河流编码" align="center" prop="code" width="120" show-overflow-tooltip />
      <el-table-column label="别名" align="center" prop="alias" width="120" show-overflow-tooltip />
      <el-table-column label="干流说明" align="center" prop="branch" width="120" show-overflow-tooltip />
      <el-table-column label="所属流域" align="center" prop="riverBasinName" width="80" show-overflow-tooltip />
      <el-table-column label="所属水系" align="center" prop="riverSystemName" width="110" show-overflow-tooltip />
      <el-table-column label="描述" align="center" prop="info" min-width="120" show-overflow-tooltip />
      <el-table-column label="流域面积(km²)" align="center" prop="area" width="110" show-overflow-tooltip />
      <el-table-column label="河长(km)" align="center" prop="length" width="110" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="primary" link icon="View" @click="handleView(scope.row)" v-hasPermi="['base:river:query']">查看</el-button>
          <el-button type="primary" link icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['base:river:edit']">修改</el-button>
          <el-button type="primary" link icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['base:river:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script  setup  name="River">
import { removeRiver, listRiver, exportRiver } from "@/api/base/river";
import staticRouter from "@/router/static";
const { proxy } = getCurrentInstance();

const buttonLoading = ref(false); //按钮loading
const loading = ref(true); // 遮罩层
const exportLoading = ref(false); // 导出遮罩层
const showSearch = ref(true); // 显示搜索条件
const riverList = ref([]); // 河流表格数据
const riverOptions = ref([]); // 河流树选项

const queryParams = ref({
  riverBasinName: undefined,
  riverSystemName: undefined,
  name: undefined,
  code: undefined,
  within: undefined,
});

/** 查询河流列表 */
function fetchList() {
  loading.value = true;
  listRiver(queryParams.value)
    .then((res) => {
      riverList.value = proxy.handleTree(res.rows, "id", "parentId");
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 转换河流数据结构 */
function normalizer(node) {
  if (node.children && !node.children.length) {
    delete node.children;
  }
  return {
    id: node.id,
    label: node.name,
    children: node.children,
  };
}
/** 搜索按钮操作 */
function handleQuery() {
  fetchList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  proxy.$router.push(staticRouter.riverAdd);
}
/** 查看按钮操作 */
function handleView(row) {
  const id = row.id || ids;
  proxy.$router.push({
    path: staticRouter.riverView,
    query: { pageFlag: "view", id: id },
  });
}
/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id || ids;
  proxy.$router.push({
    path: staticRouter.riverEdit,
    query: { pageFlag: "edit", id: id },
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  $confirm('是否确认删除河流编号为"' + row.id + '"的数据项?', "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      loading.value = true;
      return removeRiver(row.id);
    })
    .then(() => {
      loading.value = false;
      fetchList();
      $msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}
// 导出按钮操作
function handleExport() {
  $confirm("是否确认导出所有数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      const queryParams2 = Object.assign({}, queryParams.value, {
        pageNum: 1,
        pageSize: 10000,
      });
      exportLoading.value = true;
      return exportRiver(queryParams2);
    })
    .then((res) => {
      $download(res.data);
    })
    .catch(() => {})
    .finally(() => {
      exportLoading.value = false;
    });
}

fetchList();
</script>
